Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / repository / EventFirmwareRepositoryImpl.kt

Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/repository/EventFirmwareRepositoryImpl.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 6.42 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.repository

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.emitAll
Tff7b72import T7ee787kotlinx.coroutines.flow.flow
Tff7b72import T7ee787kotlinx.coroutines.flow.flowOn
Tff7b72import T7ee787kotlinx.coroutines.flow.map
Tff7b72import T7ee787kotlinx.coroutines.sync.Mutex
Tff7b72import T7ee787kotlinx.coroutines.sync.withLock
Tff7b72import T7ee787kotlinx.coroutines.withContext
Tff7b72import T7ee787kotlinx.serialization.json.Json
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.common.util.nowMillis
Tff7b72import T7ee787org.meshtastic.core.common.util.safeCatching
Tff7b72import T7ee787org.meshtastic.core.data.datasource.BundledAssetReader
Tff7b72import T7ee787org.meshtastic.core.data.datasource.EventFirmwareEditionLocalDataSource
Tff7b72import T7ee787org.meshtastic.core.data.datasource.decode
Tff7b72import T7ee787org.meshtastic.core.data.util.SingleFlightRefresher
Tff7b72import T7ee787org.meshtastic.core.database.entity.asEntity
Tff7b72import T7ee787org.meshtastic.core.database.entity.asExternalModel
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers
Tff7b72import T7ee787org.meshtastic.core.model.EventFirmwareEdition
Tff7b72import T7ee787org.meshtastic.core.model.EventFirmwareResponse
Tff7b72import T7ee787org.meshtastic.core.model.util.TimeConstants
Tff7b72import T7ee787org.meshtastic.core.network.EventFirmwareRemoteDataSource
Tff7b72import T7ee787org.meshtastic.core.repository.EventFirmwareRepository
Tff7b72import T7ee787kotlin.concurrent.Volatile
Tff7b72import T7ee787kotlin.time.Duration.Companion.minutes

T8b949e/**
* Caches event-firmware display metadata in the DB, seeded from the bundled `event_firmware.json` snapshot and
* refreshed from `/resource/eventFirmware`. DB-backed (not just in-memory) so the snapshot survives across process
* restarts without waiting on a fresh network round-trip. Mirrors [DeviceHardwareRepositoryImpl]'s seed → single-flight
* refresh pattern.
*/
Tf0883e@Single
Tff7b72class T56d364EventFirmwareRepositoryImplTb4b4b4(
Tff7b72private Tff7b72val Te6edf3remoteDataSourceTb4b4b4: Te6edf3EventFirmwareRemoteDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3assetReaderTb4b4b4: Te6edf3BundledAssetReaderTb4b4b4,
Tff7b72private Tff7b72val Te6edf3jsonTb4b4b4: Te6edf3JsonTb4b4b4,
Tff7b72private Tff7b72val Te6edf3localDataSourceTb4b4b4: Te6edf3EventFirmwareEditionLocalDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3EventFirmwareRepository Tb4b4b4{

T8b949e/** Serializes seeding so concurrent callers don't duplicate writes. */
Tff7b72private Tff7b72val Te6edf3writeMutex Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)

Tf0883e@Volatile Tff7b72private Tff7b72var Te6edf3lastRefreshMillis Tff7b72= T79c0ff0L

T8b949e// Advanced on every refresh *attempt* (success, empty, or failure), unlike lastRefreshMillis which only advances on
T8b949e// a stored non-empty payload. Without this, an offline device (a common, expected state) would re-enter the stale
T8b949e// branch and block up to NETWORK_REFRESH_TIMEOUT_MS on every getEdition() call, since the failed Deferred is no
T8b949e// longer active. The cooldown caps retries while offline without waiting the full CACHE_EXPIRATION_TIME_MS.
Tf0883e@Volatile Tff7b72private Tff7b72var Te6edf3lastAttemptMillis Tff7b72= T79c0ff0L

T8b949e/** Shared refresh; a caller that stops waiting can't abort it, and the fetch still lands in the DB. */
Tff7b72private Tff7b72val Te6edf3refresher Tff7b72=
Te6edf3SingleFlightRefresherTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4, Ta5d6ff"Ta5d6ffEventFirmwareRepositoryTa5d6ff"Tb4b4b4) Tb4b4b4{
Te6edf3lastAttemptMillis Tff7b72= Te6edf3nowMillis
Tff7b72val Te6edf3editions Tff7b72= Te6edf3remoteDataSourceTb4b4b4.Te6edf3getEventFirmwareTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3editions
Tff7b72if Tb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3isNotEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3localDataSourceTb4b4b4.Te6edf3upsertAllTb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asEntityTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Te6edf3localDataSourceTb4b4b4.Te6edf3deleteNotInTb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3edition Tb4b4b4}Tb4b4b4)
Te6edf3lastRefreshMillis Tff7b72= Te6edf3nowMillis
Tb4b4b4}
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffgetEditionTb4b4b4(Te6edf3editionNameTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3EventFirmwareEdition? Tff7b72= Te6edf3withContextTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4) Tb4b4b4{
Te6edf3ensureSeededTb4b4b4(Tb4b4b4)
Te6edf3maybeRefreshTb4b4b4(Te6edf3maxWaitMs Tff7b72= Te6edf3NETWORK_REFRESH_TIMEOUT_MSTb4b4b4)
Te6edf3localDataSourceTb4b4b4.Te6edf3getByEditionTb4b4b4(Te6edf3editionNameTb4b4b4)Tff7b72?.Te6edf3asExternalModelTb4b4b4(Tb4b4b4)
Tb4b4b4}

Tff7b72override Tff7b72fun Td2a8ffobserveEditionTb4b4b4(Te6edf3editionNameTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Te6edf3EventFirmwareEdition?Tff7b72> Tff7b72= Te6edf3flow Tb4b4b4{
Te6edf3ensureSeededTb4b4b4(Tb4b4b4)
T8b949e// Don't wait — the emissions below carry whatever the refresh writes, so blocking would only delay the
T8b949e// cached value the caller could already be showing.
Te6edf3maybeRefreshTb4b4b4(Te6edf3maxWaitMs Tff7b72= T79c0ff0Tb4b4b4)
Te6edf3emitAllTb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3observeByEditionTb4b4b4(Te6edf3editionNameTb4b4b4)Tb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTff7b72?.Te6edf3asExternalModelTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Tb4b4b4}
Tb4b4b4.Te6edf3flowOnTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4)

T8b949e/** Refreshes if the cache is stale and the retry cooldown has elapsed. See [lastAttemptMillis] for the cooldown. */
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffmaybeRefreshTb4b4b4(Te6edf3maxWaitMsTb4b4b4: Tffa657LongTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3stale Tff7b72= Te6edf3nowMillis Tff7b72- Te6edf3lastRefreshMillis Tff7b72> Te6edf3CACHE_EXPIRATION_TIME_MS
Tff7b72val Te6edf3retryCooldownElapsed Tff7b72= Te6edf3nowMillis Tff7b72- Te6edf3lastAttemptMillis Tff7b72> Te6edf3REFRESH_RETRY_COOLDOWN_MS
Tff7b72if Tb4b4b4(Te6edf3stale Tff7b72&Tff7b72& Te6edf3retryCooldownElapsedTb4b4b4) Tb4b4b4{
Te6edf3refresherTb4b4b4.Te6edf3refreshTb4b4b4(Te6edf3maxWaitMs Tff7b72= Te6edf3maxWaitMsTb4b4b4)
Tb4b4b4}
Tb4b4b4}

T8b949e/** Seeds the table from the bundled snapshot if empty (fresh install, data clear). */
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffensureSeededTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72> T79c0ff0Tb4b4b4) Tff7b72return
Te6edf3writeMutexTb4b4b4.Te6edf3withLock Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72=Tff7b72= T79c0ff0Tb4b4b4) Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{
Tff7b72val Te6edf3editions Tff7b72= Te6edf3assetReaderTb4b4b4.Te6edf3decodeTff7b72<Te6edf3EventFirmwareResponseTff7b72>Tb4b4b4(Te6edf3ASSET_NAMETb4b4b4, Te6edf3jsonTb4b4b4)Tff7b72?.Te6edf3editionsTb4b4b4.Te6edf3orEmptyTb4b4b4(Tb4b4b4)
Te6edf3localDataSourceTb4b4b4.Te6edf3upsertAllTb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asEntityTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffEventFirmwareRepository: failed to seed from bundled JSONTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Tff7b72private Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72private Tff7b72const Tff7b72val Te6edf3ASSET_NAME Tff7b72= Ta5d6ff"Ta5d6ffevent_firmware.jsonTa5d6ff"
Tff7b72private Tff7b72val Te6edf3CACHE_EXPIRATION_TIME_MS Tff7b72= Te6edf3TimeConstantsTb4b4b4.Te6edf3ONE_DAYTb4b4b4.Te6edf3inWholeMilliseconds

T8b949e/** Minimum gap between refresh *attempts*, so a failing/empty fetch (e.g. offline) doesn't retry every call. */
Tff7b72private Tff7b72val Te6edf3REFRESH_RETRY_COOLDOWN_MS Tff7b72= T79c0ff5.Te6edf3minutesTb4b4b4.Te6edf3inWholeMilliseconds

T8b949e/** Maximum time a blocking lookup waits for an in-flight refresh before returning cached/bundled data. */
Tff7b72private Tff7b72const Tff7b72val Te6edf3NETWORK_REFRESH_TIMEOUT_MS Tff7b72= T79c0ff5Te6edf3_000L
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.09s